'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    About:
    Classes for creating various kinds of dialogs.
    
    BrowserWindow Usage:
        browse = BrowserWindow( inFileMode='ExistingFiles', inFileTypes=['py'], inAcceptMode='Save' )
        browse.openBrowser()
        files = browse.selectedFiles
        
    ConfirmationWindow Usage:
        dialog = ConfirmationWindow( inText='This asset needs to be moved.',
                                     inInformativeText='Do you want to apply this change?',
                                     inRole='Apply' )
        result = con.openDialog()
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import os
from PySide import QtGui
from PySide import QtCore

'''
============================================================
--->>   BROWSER WINDOW CLASS
============================================================
'''
class BrowserWindow( object ):
    def __init__( self, inStartDirectory='c:/',
                  inFileMode='ExistingFile',
                  inFileTypes=['*'],
                  inTitle='Browse for a file',
                  inAcceptMode='Open' ):
        '''
        Creates a file or directory browser.
        
        @inStartDirectory: String. Path to the directory to start the browser.
        
        @inFileMode: String. AnyFile, The name of a file, whether it exists or not.
            ExistingFiles, The names of zero or more existing files.
            ExistingFile, The name of a single existing file.
            Directory, The name of a directory.
            
        @inFileTypes: List of Strings. Each string is a file type.
        
        @inTitle: String. Browser window title message.
        
        @inAcceptMode: String. Open, sets the accept button to the text Open.
            Save, sets the accept button to the text Save.
        '''
        self.startDirectory = inStartDirectory
        
        if inFileMode == 'AnyFile':
            self.fileMode = QtGui.QFileDialog.AnyFile
        elif inFileMode == 'ExistingFiles':
            self.fileMode = QtGui.QFileDialog.ExistingFiles
        elif inFileMode == 'ExistingFile':
            self.fileMode = QtGui.QFileDialog.ExistingFile
        elif inFileMode == 'Directory':
            self.fileMode = QtGui.QFileDialog.Directory 
            
        self.fileTypes = ''
        for fileExt in inFileTypes:
            self.fileTypes += '*.'+fileExt
        
        self.title = inTitle
        
        if inAcceptMode == 'Open':
            self.acceptMode = QtGui.QFileDialog.AcceptOpen
        elif inAcceptMode == 'Save':
            self.acceptMode = QtGui.QFileDialog.AcceptSave 
            
        self.selectedFiles = []
            
    def openBrowser( self ):
        '''
        Create the browser. Appends the class property selectedFiles with the user
        selected items (files or directory).
        '''
        browser = QtGui.QFileDialog()
        browser.setDirectory( self.startDirectory )
        browser.setFileMode( self.fileMode )
        browser.setNameFilter( self.fileTypes )
        browser.setWindowTitle( self.title )
        browser.setAcceptMode( self.acceptMode )   
        
        if browser.exec_():
            
            for filePath in browser.selectedFiles():
                
                # Unicode convert to String.
                path, ext = os.path.splitext( str(filePath) )
                
                if ext:
                    
                    finalPath = str(filePath)
                    
                else:
                    
                    # This assumes only one file extension was passed into the class.
                    # Need to handle multiple possible file extensions.
                    newExt = self.fileTypes.split( '.' )[1]
                    finalPath = str(filePath) + '.' + newExt
                
                self.selectedFiles.append( finalPath )

'''
============================================================
--->>   CONFIRMATION WINDOW CLASS
============================================================
'''
class ConfirmationWindow( object ):
    def __init__( self, inText='A thing has happened.',
                  inInformativeText='Do this?',
                  inRole='OK' ):
        '''
        Creates a simple message prompt window.
        
        @inText: String. Description of the issue.
        
        @inInformativeText: String. Description of what needs to happen.
        
        @inRole: String. Button type and layout presets.
            Save, prompt to save or discard.
            OK, prompt to confirm message.
            Open, prompt to confirm opening of file.
            Apply, prompt to confirm application of change(s).
            Yes, prompt to confirm which change to make.
            Retry, prompt to retry.
        '''
        self.text = inText
        self.informativeText = inInformativeText
        
        self.standardButtons = []
        if inRole == 'Save':
            self.standardButtons.append( QtGui.QMessageBox.Save )
            self.standardButtons.append( QtGui.QMessageBox.Discard )
        elif inRole == 'OK':
            self.standardButtons.append( QtGui.QMessageBox.Ok )
        elif inRole == 'Open':
            self.standardButtons.append( QtGui.QMessageBox.Open )
            self.standardButtons.append( QtGui.QMessageBox.Cancel )
        elif inRole == 'Apply':
            self.standardButtons.append( QtGui.QMessageBox.Apply )
            self.standardButtons.append( QtGui.QMessageBox.Abort )
            self.standardButtons.append( QtGui.QMessageBox.Cancel )
        elif inRole == 'Yes':
            self.standardButtons.append( QtGui.QMessageBox.Yes )
            self.standardButtons.append( QtGui.QMessageBox.No )
        elif inRole == 'Retry':
            self.standardButtons.append( QtGui.QMessageBox.Retry )
            self.standardButtons.append( QtGui.QMessageBox.Cancel )
            
    def openDialog( self ):
        '''
        Creates the prompt window.
        
        @return: Returns ID of button pressed.
        '''
        msgBox = QtGui.QMessageBox()
        msgBox.setText( self.text )
        msgBox.setInformativeText( self.informativeText )
        
        if len(self.standardButtons) == 1:
            msgBox.setStandardButtons( self.standardButtons[0] )
        elif len(self.standardButtons) == 2:
            msgBox.setStandardButtons( self.standardButtons[0] | self.standardButtons[1] )
        elif len(self.standardButtons) == 3:
            msgBox.setStandardButtons( self.standardButtons[0] | self.standardButtons[1] | self.standardButtons[2] )
        msgBox.setDefaultButton( self.standardButtons[0] )
        
        return msgBox.exec_()